package com.smart.mattress.account.redis;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import redis.clients.jedis.Jedis;

/**
 * redicache 工具类
 *
 */
@SuppressWarnings("unchecked")
@Service
public class RedisUtil {

    Logger logger = LoggerFactory.getLogger(RedisUtil.class);

    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
    
    private Jedis jedis;
    @Value("${spring.redis.host}")
    String ip;
    @Value("${spring.redis.port}")
    int port;
    @Value("${spring.redis.password}")
    String password;


    public void getJedis() {
        jedis = new Jedis(ip, port);
        //权限认证
        if(!StringUtils.isEmpty(password)) {
            jedis.auth(password);
        }


    }


    public void setObject(String key,Object object,int time){

        if(jedis == null){
            getJedis();
        }


        try {
            jedis.set(key.getBytes(), SerializeUtil.serialize(object));
            jedis.expire(key.getBytes(),time);//设置过期时间
        }catch (Exception e){
            logger.error("redis set object key:{},value:{},error:{}",key,object,e);
        }

    }
    
    
    
    /**
     * 批量删除对应的value
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }
    
    
    /**
     * 批量删除key
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }
    /**
     * 删除对应的value
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
    	return redisTemplate.hasKey(key);
    }
    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        logger.info("redis set key = "+key);

        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        logger.info("get key ="+key+",result = "+result);
        return result;
    }
    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
           logger.error("redis set error",e);
        }
        return result;
    }
    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @param expireTime 过期时间,单位秒
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();

            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            logger.error("redis set error",e);
        }
        return result;
    }
}
